package com.jsmtr.rms.utils;

import java.util.ArrayList;
import java.util.List;

public class MathTools {

    public static void main(String[] args) {
        List<Integer> numerator = new ArrayList<Integer>();
        List<Integer> denominator = new ArrayList<Integer>();
        numerator.add(2400);
        numerator.add(3800);
        numerator.add(3620);
        denominator.add(100);
        denominator.add(100);
        denominator.add(100);
        System.out.println(fracAdd1(numerator, denominator));

    }

    public static int fracAdd(List<Integer> numerator, List<Integer> denominator) {
        try {
            if (numerator.size() == denominator.size() && numerator.size() > 0) {
                if (numerator.size() == 1) {
                    if (numerator.get(0) == denominator.get(0)) {
                        return 1;
                    } else {
                        return 0;
                    }
                } else {
                    int first_numerator = numerator.get(0);
                    int first_denominator = denominator.get(0);
                    int i = 1;
                    for (; i < numerator.size(); i++) {
                        System.out.println(first_numerator + "/" + first_denominator);
                        System.out.println(numerator.get(i) + "/" + denominator.get(i));

                        List<Integer> result = fracAdd(first_numerator, first_denominator, numerator.get(i), denominator.get(i));
                        first_numerator = result.get(0);
                        first_denominator = result.get(1);

                        System.out.println(first_numerator + "/" + first_denominator);
                    }

                    if (first_numerator == first_denominator) {
                        return 1;
                    } else {
                        return 0;
                    }
                }
            } else {
                return 0;
            }
        } catch (Exception e) {
            return 0;
        }

    }

    public static double fracAdd1(List<Integer> numerator, List<Integer> denominator) {
        try {
            if (numerator.size() == denominator.size() && numerator.size() > 0) {
                if (numerator.size() == 1) {
                    return (double) numerator.get(0) / (double) denominator.get(0);
                } else {
                    int first_numerator = numerator.get(0);
                    int first_denominator = denominator.get(0);
                    int i = 1;
                    for (; i < numerator.size(); i++) {
                        System.out.println(first_numerator + "/" + first_denominator);
                        System.out.println(numerator.get(i) + "/" + denominator.get(i));

                        List<Integer> result = fracAdd(first_numerator, first_denominator, numerator.get(i), denominator.get(i));
                        first_numerator = result.get(0);
                        first_denominator = result.get(1);

                        System.out.println(first_numerator + "/" + first_denominator);
                    }
                    return (double) first_numerator / (double) first_denominator;
                }
            } else {
                return 0;
            }
        } catch (Exception e) {
            return 0;
        }

    }


    public static List<Integer> fracAdd(int first_numerator, int first_denominator, int second_numrator, int second_denominator) {
        int denominator;//分母
        int numerator;//分子  

        if (first_denominator == second_denominator)  //分母相同时加分子
        {
            denominator = first_denominator;
            numerator = first_numerator + second_numrator;
        } else  //否则同分比较分子
        {
            denominator = first_denominator * second_denominator;
            numerator = first_numerator * second_denominator + first_denominator * second_numrator;
        }
        int gcd = gcd(numerator, denominator);
        denominator = denominator / gcd;
        numerator = numerator / gcd;
        List<Integer> list = new ArrayList<Integer>();
        list.add(numerator);
        list.add(denominator);
        return list;
    }

    static int gcd(int x, int y) {
        int r;
        while (y != 0) {
            r = x % y;
            x = y;
            y = r;
        }
        return x;
    }
}